using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections.Generic;

public class AttackPopup : MonoBehaviour
{
	public Gradient colorChange;
	
	public Image head;
	public Image lArm;
	public Image rArm;
	public Image torso;
	public Image lLeg;
	public Image rLeg;

	private bool clicked;
	private BodyType typeClicked;
	private float accuracy = 50f;
	private CharacterInfo attacker;
	private Character target;
	private float [] wantedValues;
	private float [] currentValues;
	protected List<Tile> pathForInteraction;

	private void Awake()
	{
		AddDelegateEnter(head);
		AddDelegateExit(head);
		AddDelegateClick(head);
		
		AddDelegateEnter(lArm);
		AddDelegateExit(lArm);
		AddDelegateClick(lArm);
		
		AddDelegateEnter(rArm);
		AddDelegateExit(rArm);
		AddDelegateClick(rArm);
		
		AddDelegateEnter(torso);
		AddDelegateExit(torso);
		AddDelegateClick(torso);
		
		AddDelegateEnter(lLeg);
		AddDelegateExit(lLeg);
		AddDelegateClick(lLeg);
		
		AddDelegateEnter(rLeg);
		AddDelegateExit(rLeg);
		AddDelegateClick(rLeg);

		wantedValues = new float[6];
		currentValues = new float[6];
	}

	public void Init(Character _target, List<Tile> _path)
	{
		target = _target;
		attacker = InitiativeManager.Instance.current.GetInfo<CharacterInfo>();
		accuracy = attacker.precision.GetRandomValue();
		pathForInteraction = _path;

		clicked = false;
	}

	private void Update()
	{
		for (int i = 0; i < currentValues.Length; i++)
		{
			currentValues[i] = Mathf.Lerp(currentValues[i], wantedValues[i], Time.deltaTime * 3f);
		}
		
		head.color = colorChange.Evaluate(currentValues[0]);
		lArm.color = colorChange.Evaluate(currentValues[1]);
		rArm.color = colorChange.Evaluate(currentValues[2]);
		torso.color = colorChange.Evaluate(currentValues[3]);
		lLeg.color = colorChange.Evaluate(currentValues[4]);
		rLeg.color = colorChange.Evaluate(currentValues[5]);

		if (clicked && Mathf.Abs(currentValues[(int)typeClicked] - wantedValues[(int)typeClicked]) < 0.1f)
		{
			InitiativeManager.Instance.current.PerformAttack(target, typeClicked, pathForInteraction);
			Debug.Log("Attack Zombie!");
			ClosePopup();
		}
	}

	public void ClosePopup()
	{
		ScreensManager.Instance.RemovePopup(gameObject);
	}

	public void MouseEnter(Image _data)
	{
		if (!clicked)
		{
			if (_data == head)
				wantedValues = StatBody.CalculateDamage(BodyType.Head, accuracy, 1f);
			else if (_data == lArm)
				wantedValues = StatBody.CalculateDamage(BodyType.L_Arm, accuracy, 1f);
			else if (_data == rArm)
				wantedValues = StatBody.CalculateDamage(BodyType.R_Arm, accuracy, 1f);
			else if (_data == torso)
				wantedValues = StatBody.CalculateDamage(BodyType.Torso, accuracy, 1f);
			else if (_data == lLeg)
				wantedValues = StatBody.CalculateDamage(BodyType.L_Leg, accuracy, 1f);
			else // _data == rLeg
				wantedValues = StatBody.CalculateDamage(BodyType.R_Leg, accuracy, 1f);
		}
		//Debug.Log(values[0] + " " + values[1] + " " + values[2] + " " + values[3] + " " + values[4] + " " + values[5]);
	}

	public void MouseExit(Image _data)
	{
		if (!clicked)
		{
			wantedValues = new float[6];
		}
	}

	public void MouseClick(Image _data)
	{
		if (_data == head)
			typeClicked = BodyType.Head;
		else if (_data == lArm)
			typeClicked = BodyType.L_Arm;
		else if (_data == rArm)
			typeClicked = BodyType.R_Arm;
		else if (_data == torso)
			typeClicked = BodyType.Torso;
		else if (_data == lLeg)
			typeClicked = BodyType.L_Leg;
		else // _data == rLeg
			typeClicked = BodyType.R_Leg;

		clicked = true;
	}
	
	private void AddDelegateEnter(Image _image)
	{
		EventTrigger trigger = _image.GetComponent<EventTrigger>();

		EventTrigger.TriggerEvent evt = new EventTrigger.TriggerEvent();
		evt.AddListener((eventData) => MouseEnter(_image));
		
		EventTrigger.Entry entry = new EventTrigger.Entry();
		entry.eventID = EventTriggerType.PointerEnter;
		entry.callback = evt;
		trigger.delegates.Add(entry);
	}
	
	private void AddDelegateExit(Image _image)
	{
		EventTrigger trigger = _image.GetComponent<EventTrigger>();
		
		EventTrigger.TriggerEvent evt = new EventTrigger.TriggerEvent();
		evt.AddListener((eventData) => MouseExit(_image));
		
		EventTrigger.Entry entry = new EventTrigger.Entry();
		entry.eventID = EventTriggerType.PointerExit;
		entry.callback = evt;
		trigger.delegates.Add(entry);
	}
	
	private void AddDelegateClick(Image _image)
	{
		EventTrigger trigger = _image.GetComponent<EventTrigger>();
		
		EventTrigger.TriggerEvent evt = new EventTrigger.TriggerEvent();
		evt.AddListener((eventData) => MouseClick(_image));
		
		EventTrigger.Entry entry = new EventTrigger.Entry();
		entry.eventID = EventTriggerType.PointerClick;
		entry.callback = evt;
		trigger.delegates.Add(entry);
	}
}
